local assets = {
	Asset("ANIM", "anim/npc_fx_forcefield.zip"),
}

-----------------------------------------------------------------------------------------------
--- 灯光相关的控制
-- FRAMES
local function Light_Task_Start(inst)
	if not TheWorld.ismastersim then
		return
	end
    if not inst:HasTag("with_light") then
        return
    end
	-- TheWorld.state.isday
	-- TheWorld.state.isdusk
	-- TheWorld.state.isnight
	local delta_num = 0.0025
	if TheWorld.state.isnight == true or TheWorld:HasTag("cave") then
		-- print("info:Light_Task_Start  On",inst.__light_up_num)

		----------------------------------------------------------
		-- 关掉旧的任务
		if inst.__light_up_task ~= nil then
			inst.__light_up_task:Cancel()
			inst.__light_up_task = nil
		end
		if inst.__light_off_task ~= nil then
			inst.__light_off_task:Cancel()
			inst.__light_off_task = nil
		end
		-----------------------------------------------------------

		inst.Light:Enable(true)

		if inst.__light_up_num == nil then
			inst.__light_up_num = 0
		end

		inst.__light_up_task = inst:DoPeriodicTask(FRAMES,function()
			if inst.__light_up_num >= inst.__max_Intensity then
				inst.__light_up_task:Cancel()
				inst.__light_up_task = nil
				return
			end
			inst.Light:SetIntensity(inst.__light_up_num )
			inst.__light_up_num = inst.__light_up_num + delta_num
		end)
	elseif TheWorld.state.isday then	----------------- light  off
		-- print("info:Light_Task_Start  off")

		if inst.__light_up_task then
			inst.__light_up_task:Cancel()
			inst.__light_up_task = nil
		end

		if inst.__light_up_num == nil or inst.__light_up_num <= 0 then
			inst.Light:Enable(false)
			return
		end

		if inst.__light_up_num > 0 then
			inst.__light_off_task = inst:DoPeriodicTask(FRAMES,function()
				if inst.__light_up_num < 0 then
					inst.Light:Enable(false)
					if inst.__light_off_task then
						inst.__light_off_task:Cancel()
						inst.__light_off_task = nil
					end
					return
				end
				inst.Light:SetIntensity(inst.__light_up_num)
				inst.__light_up_num = inst.__light_up_num  - delta_num
			end)
		end
	else


	end
end
-----------------------------------------------------------------------------------------------

local function fx()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()
    inst.entity:AddLight()

    inst:AddTag("INLIMBO")
    inst:AddTag("FX")

    inst.Light:Enable(false)
    inst.Light:SetRadius(3)
    inst.Light:SetFalloff( 0.58)
    -- inst.Light:SetIntensity(0.75)
    inst.__max_Intensity = 0.75001
    inst.Light:SetIntensity(0)
    inst.Light:SetColour(200 / 255, 200 / 255, 200 / 255)
    inst:DoTaskInTime(0.5,function()
		Light_Task_Start(inst)
	end)

    inst.AnimState:SetBank("forcefield")
    inst.AnimState:SetBuild("forcefield")
    inst.AnimState:OverrideSymbol("circle alpha", "npc_fx_forcefield", "circle alpha")
    inst.AnimState:OverrideSymbol("circle trim", "npc_fx_forcefield", "circle trim")
    inst.AnimState:PlayAnimation("open")
    inst.AnimState:PushAnimation("idle_loop", true)


    if not TheWorld.ismastersim then
        return inst
    end
    -- -----------------------------------------------------------------------------------
    inst:WatchWorldState("isnight",function()	-------- 晚上
        if TheWorld.state.isnight then
            inst:DoTaskInTime(3,function()
                Light_Task_Start(inst)			
            end)
        end
    end)
    inst:WatchWorldState("cycles",function()	-------- 新的一天
        inst:DoTaskInTime(5,function()
            Light_Task_Start(inst)			
        end)
    end)
    -- -- -----------------------------------------------------------------------------------
    inst:ListenForEvent("Set",function(inst,_table)
        -- _table = {
        --     pt = Vector3(0,0,0),
        --     color = Vector3(255,255,255),
        --     scale = Vector3(0,0,0),
        --     MultColour_Flag = nil,
        --     a = 0.1,
        --     light = nil,
        --     light_color = Vector3(0,0,0)
        -- }
        if _table == nil then
            return
        end
        if _table.pt and _table.pt.x then
            inst.Transform:SetPosition(_table.pt.x,_table.pt.y,_table.pt.z)
        end

        if _table.color and _table.color.x then
            if _table.MultColour_Flag ~= true then
                inst:AddComponent("colouradder")
                inst.components.colouradder:OnSetColour(_table.color.x/255 , _table.color.y/255 , _table.color.z/255 , _table.a or 1)
            else
                inst.AnimState:SetMultColour(_table.color.x,_table.color.y, _table.color.z, _table.a or 1)
            end
        end
        if _table.scale and _table.scale.x then
            inst.AnimState:SetScale(_table.scale.x,_table.scale.y,_table.scale.z)
        end
        if _table.light == true then
            inst:AddTag("with_light")
        end
        if _table.light_color  and _table.light_color.x then
            inst.Light:SetColour(_table.light_color.x / 255, _table.light_color.y / 255, _table.light_color.z / 255)
        end

        inst.Ready = true
    end)
    inst:DoTaskInTime(0,function( ... )
        if inst.Ready ~= true then
            inst:Remove()
        end
    end)

    return inst
end

return Prefab("npc_fx_forcefield",fx,assets)